package com.lv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lv.common.dto.MenuList;
import com.lv.entity.Role;
import com.lv.entity.RoleMenu;
import com.lv.mapper.RoleMapper;
import com.lv.service.RoleMenuService;
import com.lv.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凉薄
 * @since 2022-01-06
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Override
    public void deleteRole(Long id) {
        removeById(id);
        roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id",id));
    }

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleMenuService roleMenuService;
    @Override
    public IPage<Role> getAllRole(Integer current, Integer pageSize,String roleName) {
        IPage<Role> page=new Page<>(current,pageSize);
        LambdaQueryWrapper<Role> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(roleName),Role::getName,roleName);
        return page(page,wrapper);
    }

    @Override
    public List<Long> getRoleInfo(Long id) {
        return roleMapper.getRoleInfo(id);
    }

    @Override
    public void upRoleInfo(Long id, Long[] menuIds) {
        roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id",id));
        List<RoleMenu> roleMenus=new ArrayList<>();
        for (Long menuId : menuIds) {
            roleMenus.add(new RoleMenu(id,menuId));
        }
        roleMenuService.saveBatch(roleMenus);
    }
}
